package com.sskj.one.login;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.hjq.toast.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.one.R;
import com.sskj.one.view.IVideoView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/29
 */
public class VideoActivity extends BaseActivity<VideoPresenter> {
    @BindView(R.id.video_view)
    VideoView videoView;
    @BindView(R.id.tvTitle)
    TextView tvTitle;

    private String videoType;
    private String videoSavePath;

    private HashMap<String, String> videoPaths = new HashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_video;
    }

    @Override
    public VideoPresenter getPresenter() {
        return new VideoPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("视频介绍");
        videoType = getIntent().getStringExtra(Constans.VIDEO_TYPE);
        MediaController mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        videoView.requestFocus();
    }

    @Override
    public void initData() {
        videoPaths.put("bizhifu", "https://onexm1.oss-cn-beijing.aliyuncs.com/appmp4/bizhifu.mp4");
        videoPaths.put("offline", "https://onexm1.oss-cn-beijing.aliyuncs.com/appmp4/offline.mp4");
        videoPaths.put("wanfa", "https://onexm1.oss-cn-beijing.aliyuncs.com/appmp4/wanfa.mp4");
        switch (videoType) {
            case Constans.VIDEO_WANFA:
                videoSavePath = SPUtil.get(SPConfig.VIDEO_PATH_WANFA, "");
                break;
            case Constans.VIDEO_OFFLINE:
                videoSavePath = SPUtil.get(SPConfig.VIDEO_PATH_OFFLINE, "");
                break;
            case Constans.VIDEO_BIZHIFU:
                videoSavePath = SPUtil.get(SPConfig.VIDEO_PATH_BIZHIFU, "");
                break;
        }
        if (isEmpty(videoSavePath)) {
            loadVideo(videoPaths.get(videoType));
        } else {
            videoView.setVideoPath(videoSavePath);
            videoView.start();
        }
    }

    private void loadVideo(String urlPath) {
        new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(isGrant -> {
                    if (isGrant) {
                        OkGo.<File>get(urlPath).tag(this).execute(new FileCallback() {
                            @Override
                            public void onSuccess(Response<File> response) {
                                videoView.setVideoPath(response.body().getPath());
                                videoView.start();
                                switch (videoType) {
                                    case Constans.VIDEO_WANFA:
                                        SPUtil.put(SPConfig.VIDEO_PATH_WANFA, response.body().getPath());
                                        break;
                                    case Constans.VIDEO_OFFLINE:
                                        SPUtil.put(SPConfig.VIDEO_PATH_OFFLINE, response.body().getPath());
                                        break;
                                    case Constans.VIDEO_BIZHIFU:
                                        SPUtil.put(SPConfig.VIDEO_PATH_BIZHIFU, response.body().getPath());
                                        break;
                                }
                            }

                            @Override
                            public void onStart(Request<File, ? extends Request> request) {
                                super.onStart(request);
                                showLoading();
                                ToastUtils.show("视频文件准备中");
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                hideLoading();
                            }

                            @Override
                            public void onError(Response<File> response) {
                                super.onError(response);
                                ToastUtils.show("下载失败");
                            }
                        });
                    } else {
                        PermissionTipUtil.tip(this, "存储");
                    }
                });
    }

    public static void start(Context context, String videoType) {
        Intent intent = new Intent(context, VideoActivity.class);
        intent.putExtra(Constans.VIDEO_TYPE, videoType);
        context.startActivity(intent);
    }
}
